package com.dcbeida.activity.purchase;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.CaptureActivity;
import com.dcbeida.activity.source_unit.SourceActivity;
import com.dcbeida.activity.store.StoreActivity;
import com.dcbeida.bean.DrugBeanWithPand;
import com.dcbeida.bean.SourceBean;
import com.dcbeida.utils.Constants;
import com.dcbeida.utils.Utils;

import butterknife.Bind;

public class PurchaseExitActivity extends BaseActivity implements PurchaseExitContract.View,
        View.OnClickListener {


    @Bind(R.id.img_back)
    ImageView img_back;

    @Bind(R.id.tv_time1)
    TextView tv_time1;

    @Bind(R.id.ll_lhdw)
    LinearLayout ll_lhdw;

    @Bind(R.id.tv_lhdw)
    TextView tv_lhdw;

    @Bind(R.id.et_price)
    EditText et_price;

    @Bind(R.id.lin_add)
    LinearLayout lin_add;

    @Bind(R.id.lin_confim)
    LinearLayout lin_confim;

    @Bind(R.id.tv_confim)
    TextView tv_confim;

    @Bind(R.id.tvAddCode)
    TextView tvAddCode;

    String from = "";
    int fromID = 0;

    String invoiceMoney = "";
    private PurchaseExitContract.Present mPresent;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_purchase_exit;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initListeners();
        mPresent = new PurchaseExitPresent(this, this);
    }

    @Override
    public void setData() {
        super.setData();
        tv_time1.setText(Utils.getNowTime());
    }

    @Override
    public void setPresenter(PurchaseExitContract.Present presenter) {

    }

    private void initListeners() {
        img_back.setOnClickListener(this);
        lin_confim.setOnClickListener(this);
        tv_confim.setOnClickListener(this);
        ll_lhdw.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.lin_confim:
                Bundle b = new Bundle();
                b.putInt("type", CaptureActivity.TYPE_RE);
                showActivityForResult(CaptureActivity.class, b, Constants.RESULT_CAPTER);
                break;

            case R.id.ll_lhdw:
                b = new Bundle();
                showActivityForResult(SourceActivity.class, b, Constants.HOSPITAL_TYPE);
                break;

            case R.id.tv_confim:
                if (et_price.getText().toString().trim().length() != 0)
                    invoiceMoney = Utils.dFormat(Double.parseDouble(et_price.getText().toString().trim()));
                else {
                    showTip("请输入金额");
                    return;
                }
                if (mPresent.isDrugInfoList()) {
                    showTip("请扫码盘点");
                    return;
                }



                new AlertDialog.Builder(this).setMessage(R.string.message)
                        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setPositiveButton(R.string.confim, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        mPresent.stockinreturn(from, fromID + "", invoiceMoney);

                    }
                }).show();


                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(PurchaseExitActivity.class.getName(), "#########requestCode:" + requestCode + ";######resultCode:" + resultCode);
        if (resultCode == RESULT_OK && requestCode == Constants.RESULT_CAPTER) {
            String code = data.getStringExtra("code");
            Log.i(StoreActivity.class.getName(), "---" + code);
            mPresent.addinventorydrugcodes(code);
        } else if (requestCode == Constants.HOSPITAL_TYPE && resultCode == RESULT_OK) {//选择来货单位
            Bundle bundle = data.getExtras();
            if (null != bundle) {
                if (bundle.containsKey("sourceBean")) {
                    SourceBean sourceBean = (SourceBean) bundle.getSerializable("sourceBean");
                    fromID = sourceBean.getProviderid();
                    from = sourceBean.getProvidername();
                    tv_lhdw.setText(from);
                }
            }

        }
    }


    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }

    @Override
    public void addItem(int pos, DrugBeanWithPand info, int head_pos, DrugBeanWithPand headInfo) {
        LinearLayout itemView = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.has_big_title_with_item, null);
        LinearLayout lin_item_head = (LinearLayout) itemView.findViewById(R.id.lin_item_head);
        TextView tv_yp_name = (TextView) itemView.findViewById(R.id.tv_yp_name);
        TextView tv_count = (TextView) itemView.findViewById(R.id.tv_count);
        TextView tv_ph = (TextView) itemView.findViewById(R.id.tv_ph);
        TextView tv_yxq = (TextView) itemView.findViewById(R.id.tv_yxq);
        TextView tv_bh = (TextView) itemView.findViewById(R.id.tv_bh);
        TextView tv_box = (TextView) itemView.findViewById(R.id.tv_box);

        View headView = lin_add.getChildAt(head_pos);
        if (headView != null) {
            TextView tv_count_head = (TextView) headView.findViewById(R.id.tv_count);
            tv_count_head.setText(headInfo.getCount() + "");
        } else {
            tv_count.setText(headInfo.getCount() + "");
        }

        tv_yxq.setText(info.getExpiredate());
        tv_bh.setText(info.getDrugcode());
        tv_box.setText(info.getQuantity() + "");
        tv_ph.setText(info.getBatchnumber());
        tv_yp_name.setText(headInfo.getDrugname());

        if (info.isFirst()) {
            lin_item_head.setVisibility(View.VISIBLE);
        } else
            lin_item_head.setVisibility(View.GONE);

        lin_add.addView(itemView, pos);

        if (lin_add.getChildCount() > 0) {
            tvAddCode.setText(R.string.again3);

        }
    }

    @Override
    public void showSuccess() {
        sendBroadcast(new Intent(Constants.RECEIVER_STOCK_FRESH));
        showTip("采购退货成功");
        finish();
    }
}
